using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 游戏数据管理类
/// </summary>
public class GameDataManager
{
    private static GameDataManager instance = new GameDataManager();
    public static GameDataManager Instance => instance;
    public MusicData musicData;
    public RankList rankData;
    private GameDataManager() 
    {
        //初始化音效设置数据
        musicData = DataManager.Instance.LoadData(typeof(MusicData), "Music") as MusicData;
        //如果是第一次进入游戏 初始化数据
        if (!musicData.notFristStart)
        {
            musicData.notFristStart = true;
            musicData.isOpenBGM = true;
            musicData.isOpenSound = true;
            musicData.bgmValue = 1;
            musicData.soundValue = 1;
            DataManager.Instance.SaveData(musicData, "Music");
        }
        //初始化排行榜数据
        rankData = DataManager.Instance.LoadData(typeof(RankList), "Rank") as RankList;
    }

    //开启或关闭背景音乐
    public void OpenOrCloseBGM(bool isOen)
    {
        musicData.isOpenBGM = isOen;
        BgmManger.Instance.ChangeOpen(isOen);
        DataManager.Instance.SaveData(musicData, "Music");
    }
    //开启或关闭音效
    public void OpenOrCloseSound(bool isOen)
    {
        musicData.isOpenSound = isOen;
        DataManager.Instance.SaveData(musicData, "Music");
    }
    //改变背景音乐大小
    public void ChangeBGMValue(float value)
    {
        musicData.bgmValue = value;
        BgmManger.Instance.ChangeValue(value);
        DataManager.Instance.SaveData(musicData, "Music");
    }
    //改变音效大小
    public void ChangeSoundValue(float value)
    {
        musicData.soundValue = value;
        DataManager.Instance.SaveData(musicData, "Music");
    }
    
    //添加排行榜数据
    public void AddRankInfo(string name, int score, float time)
    {
        rankData.list.Add(new RankInfo(name, score, time));
        //排序
        rankData.list.Sort((a, b) => a.time < b.time ? -1 : 1);
        //移除10条以外的数据
        for (int i = rankData.list.Count - 1; i >= 10; i--)
        {
            rankData.list.RemoveAt(i);
        }
        DataManager.Instance.SaveData(rankData, "Rank");
    }
}
